from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views.generic import DetailView, ListView, TemplateView
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from .models import Artist, Album, Song, BandMember

# Create your views here.
class ArtistsView(ListView):
    template_name = 'website/artists.html'
    context_object_name = 'artists'

    def get_queryset(self):
        return Artist.objects.all()


class ArtistView(DetailView):
    model = Artist
    template_name = 'website/artist.html'
    pk_url_kwarg = 'artist_handle'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['albums'] = Album.objects.filter(artist=self.kwargs['artist_handle'])
        if(self.request.user.is_authenticated):
            context['member'] = BandMember.objects.filter(artist=self.kwargs['artist_handle'], member=self.request.user).exists()
        else:
            context['member'] = False
        return context


class ArtistCreate(LoginRequiredMixin, CreateView):
    model = Artist
    fields = ['name', 'handle']
    template_name = 'website/artist_create.html'

    def form_valid(self, form):
        a = form.save()
        BandMember.objects.create(member=self.request.user, artist=a)
        return HttpResponseRedirect(reverse('artist', kwargs={'artist_handle': a.handle}))


class ArtistUpdate(UserPassesTestMixin, UpdateView):
    model = Artist
    fields = ['name', 'bio', 'logo']
    template_name = 'website/artist_update.html'
    pk_url_kwarg = 'artist_handle'
    raise_exception = True

    # Only allow band members to edit the Artist page
    def test_func(self):
        if self.request.user.is_authenticated:
            return BandMember.objects.filter(artist=self.kwargs['artist_handle'], member=self.request.user).exists()
        else:
            return False


class ArtistDelete(UserPassesTestMixin, DeleteView):
    model = Artist
    tempate_name = 'website/artist_confirm_delete.html'
    pk_url_kwarg = 'artist_handle'
    raise_exception = True
    success_url = '/artists'

    # Only allow band members to edit the Artist page
    def test_func(self):
        if self.request.user.is_authenticated:
            return BandMember.objects.filter(artist=self.kwargs['artist_handle'], member=self.request.user).exists()
        else:
            return False

class AlbumView(DetailView):
    model = Album
    template_name = 'website/album.html'

    def get_queryset(self):
        self.artist = get_object_or_404(Artist, handle=self.kwargs['artist_handle'])
        return Album.objects.filter(handle=artist_handle)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['songs'] = Song.objects.filter(artist=self.kwargs['artist_handle'], album__slug__contains=self.kwargs['album_slug'])
        return context

    def get_object(self):
        return get_object_or_404(Album, slug=self.kwargs['album_slug']) 


class IndexView(TemplateView):
    template_name = 'website/index.html'


class NavbarView(TemplateView):
    template_name = 'website/navbar.html'


class WelcomeView(TemplateView):
    template_name = 'website/welcome.html'
